<?php

/**
 * @see Zend_Validate_Abstract
 */
require_once 'Zend/Validate/Abstract.php';


class Core_Validate_UniquePagename extends Zend_Validate_Abstract
{
	/**
	 * Validation failure message key for when the value is not unique.
	 */
	const NOT_UNIQUE = 'notUniquePagename';

	/**
	 * Validation failure message template definitions
	 *
	 * @var array
	 */
	protected $_messageTemplates = array(
		self::NOT_UNIQUE	=> "The url '%value%' already exists. Please choose another."
	);

	/**
	 * Defined by Zend_Validate_Interface
	 *
	 * Returns true if and only if $value contains only alphabetic and digit characters
	 *
	 * @param  string $value
	 * @return boolean
	 */
	public function isValid($value, $context = 'null')
	{
		$this->_setValue( $value );

		$page = Core_Page::factory( $value );

		if ( !isset($page) )
		{
			// If the page is empty.
			return true;
		}
		else if (is_array($context) && isset($context['pageId']) && $page->pageId == $context['pageId'])
		{
			// Valid if the retrieved page from the factory has the same ID as the one we're trying to edit.
			return true;
		}
			
		$this->_error(self::NOT_UNIQUE);
		return false;
	}
}
